package com.yuma.tingshu.user.api;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuma.tingshu.common.annotation.YuMaLogin;
import com.yuma.tingshu.common.result.Result;
import com.yuma.tingshu.model.user.UserInfo;
import com.yuma.tingshu.model.user.UserSubscribe;
import com.yuma.tingshu.user.service.UserInfoService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 根据用户id查询用户信息
     *
     * @param userId
     * @return
     */
    @GetMapping("getUserById/{userId}")
    public Result<UserInfo> getUserById(@PathVariable Long userId) {
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }

    /**
     * 根据专辑id查询当前用户是否购买过该专辑
     *
     * @return
     */
    @YuMaLogin
    @GetMapping("isPaidAlbum/{albumId}")
    public Result<Boolean> isPaidAlbum(@PathVariable Long albumId) {
        Boolean flag = this.userInfoService.isPaidAlbum(albumId);
        return Result.ok(flag);
    }

    /**
     * 根据专辑id查询当前用户购买过该专辑下的声音列表
     *
     * @return
     */
    @YuMaLogin(required = false)
    @GetMapping("getPaidTracksIdByAlbum/{albumId}")
    public Result<List<Long>> getPaidTracksIdByAlbum(@PathVariable Long albumId) {
        List<Long> trackIds = userInfoService.getPaidTracksIdByAlbum(albumId);
        return Result.ok(trackIds);
    }

    // 更新VIP状态：不是管理员也不是普通用户(服务service-dispatch)
    @PostMapping("/updateVipExpireStat")
    public Result updateVipExpireStat() {
        userInfoService.updateVipExpireStat();
        return Result.ok();
    }

    /**
     * 查询用户订阅
     * @param page
     * @param limit
     * @return
     */
    @YuMaLogin
    @GetMapping("/findUserSubscribePage/{page}/{limit}")
    public Result<Page<UserSubscribe>> findUserSubscribePage(@PathVariable Integer page, @PathVariable Integer limit) {
        Page<UserSubscribe> pageModel = userInfoService.findUserSubscribePage(page, limit);
        return Result.ok(pageModel);
    }

}

